package com.wei.frame.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Outline;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;


/**
 * 作者:赵若位
 * 时间:2019/8/21 15:33
 * 联系方式：1070138445
 * 功能:
 */
public class BaseViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mSparseArray;
    private Context mContext;

    private BaseViewHolder(@Nullable View itemView) {
        super(itemView);
        mContext = itemView.getContext();
        mSparseArray = new SparseArray<>();
    }


    public static BaseViewHolder getViewHolder(@NonNull View itemView) {
        return new BaseViewHolder(itemView);
    }


    public <T extends View> T getView(@IdRes int resourceID) {
        View view = mSparseArray.get(resourceID);
        if (view == null) {
            view = itemView.findViewById(resourceID);
            if (view != null) {
                mSparseArray.put(resourceID, view);
            }
        }
        return (T) view;
    }

    /*TextView Set Txt*/
    public BaseViewHolder setSelected(@IdRes int resourceID, boolean selected) {
        if (getView(resourceID) != null) {
            getView(resourceID).setSelected(selected);
        }
        return this;
    }

    /*TextView Set Txt*/
    public BaseViewHolder setProgressBar(@IdRes int resourceID, @Nullable int progress) {
        if (getView(resourceID) != null) {
            ProgressBar bar = getView(resourceID);
            bar.setProgress(progress);
        }
        return this;
    }


    /*TextView Set Txt*/
    public BaseViewHolder setText(@IdRes int resourceID, @Nullable String text) {
        if (getView(resourceID) != null) {
            TextView tv = getView(resourceID);
            tv.setText(text);
        }
        return this;
    }

    /*TextView Set SpannableString*/
    public BaseViewHolder setText(@IdRes int resourceID, @Nullable SpannableString paramString) {
        if (getView(resourceID) != null) {
            TextView tv = getView(resourceID);
            tv.setText(paramString);
        }
        return this;
    }

    /*TextView set CharSequence*/
    public BaseViewHolder setText(@IdRes int resourceID, @StringRes int stringRes) {
        return setText(resourceID, mContext.getResources().getString(stringRes));
    }

    public BaseViewHolder setTextTypeface(@IdRes int resourceID, @Nullable Typeface typeface) {
        if (getView(resourceID) != null) {
            TextView tv = getView(resourceID);
            tv.setTypeface(typeface);
        }
        return this;
    }

    public BaseViewHolder setTextColor(@IdRes int resourceID, @ColorRes int colorRes) {
        View view = getView(resourceID);
        if (view != null && view instanceof TextView) {
            ((TextView) view).setTextColor(mContext.getResources().getColor(colorRes));
        }
        return this;
    }


    public BaseViewHolder setTextSize(@IdRes int resourceID, @DimenRes int spSize) {
        View view = getView(resourceID);
        if (view != null && view instanceof TextView) {
            ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    mContext.getResources().getDimensionPixelSize(spSize));
        }
        return this;
    }

    public BaseViewHolder setBackground(@IdRes int resourceID, @DrawableRes int id) {
        if (getView(resourceID) != null) {
            getView(resourceID).setBackground(mContext.getDrawable(id));
        }
        return this;
    }

    public BaseViewHolder setBackgroundColor(@IdRes int resourceID, @ColorInt int id) {
        if (getView(resourceID) != null) {
            getView(resourceID).setBackgroundColor(id);
        }
        return this;
    }


    /*ImageView set Url*/
    public BaseViewHolder setImageResource(@IdRes int resourceID, @Nullable String url) {
        if (!TextUtils.isEmpty(url)) {
            ImageView view = getView(resourceID);
            if (null != view) {
                Glide.with(mContext)
                        .asBitmap()
                        .centerCrop()
                        .load(url)
                        .format(DecodeFormat.PREFER_RGB_565)
                        .override(view.getWidth(), view.getHeight())
                        .into(view);
            }
        }
        return this;
    }


    /*ImageView set Bitmap*/
    public BaseViewHolder setImageResource(@IdRes int resourceID, @DrawableRes int id) {
        if (getView(resourceID) != null) {
            Glide.with(mContext)
                    .asBitmap()
                    .format(DecodeFormat.PREFER_RGB_565)
                    .centerCrop()
                    .load(id)
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }


    /*ImageView set Bitmap*/
    public BaseViewHolder setImageResource(@IdRes int resourceID, @Nullable Bitmap bitmap) {
        if (getView(resourceID) != null && bitmap != null) {
            ImageView image = getView(resourceID);
            image.setImageBitmap(bitmap);
        }
        return this;
    }

    /*图片加载圆角图片*/
    public BaseViewHolder setImageRoundResource(@IdRes int resourceID, @Nullable String url, @DimenRes int round) {
        if ((!TextUtils.isEmpty(url)) && getView(resourceID) != null) {
            ImageView view = getView(resourceID);
            view.setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    int radius = mContext.getResources().getDimensionPixelSize(round);
                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), radius);
                }
            });
            view.setClipToOutline(true);
            setImageResource(resourceID, url);
        }
        return this;
    }


    /*ImageView set Circle Image*/
    public BaseViewHolder setCircleResource(@IdRes int resourceID, @Nullable String url) {
        if (getView(resourceID) != null) {
            Glide.with(mContext)
                    .asBitmap()
                    .load(url)
                    .circleCrop()
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }


    /*ImageView set Circle Image*/
    public BaseViewHolder setCircleResource(@IdRes int resourceID, @DrawableRes int id) {
        if (getView(resourceID) != null) {
            Glide.with(mContext)
                    .asBitmap()
                    .circleCrop()
                    .load(id)
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }

    /*User CircleImageView */
    public BaseViewHolder setCircleUserResource(@IdRes int resourceID, @Nullable String url) {
        if (getView(resourceID) != null) {
            Glide.with(mContext)
                    .asBitmap()
                    .circleCrop()
                    .load(url)
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }

    /*View set Visibility*/
    public BaseViewHolder setRating(@IdRes int resourceID, float rating) {
        if (getView(resourceID) != null) {
            RatingBar bar = getView(resourceID);
            bar.setRating(rating);
        }
        return this;
    }


    /*View set Visibility*/
    public BaseViewHolder setVisibility(@IdRes int resourceID, int visibility) {
        if (getView(resourceID) != null) {
            getView(resourceID).setVisibility(visibility);
        }
        return this;
    }


    /*设置点击事件监听*/
    public BaseViewHolder setOnClickListener(@IdRes int resourceID, @Nullable View.OnClickListener listener) {
        if (getView(resourceID) != null && listener != null) {
            getView(resourceID).setOnClickListener(listener);
        }
        return this;
    }


}
